package axelrod;

import java.util.Arrays;

public class CondPAgent implements AxelrodAgent {

    private int myLastMove;
    
    //frequency of conditional moves
    private int[][] noCondMoves = {{0, 0}, {0, 0}};
    //gain of the agent
    private final int[][] agentGain = {{3, 0}, {4, 1}};
    //the probability to chose apriori based on the gain
    private final double[] ap = {0.375, 0.625};

    public int getMove(int currentTurn, int opponentMove) {
        double max = 0;
        int move = Environment.MOVE_COOPERATE;
        double gain;
        if(currentTurn ==  0) {
            myLastMove = move;
            return move;
        }
        noCondMoves[myLastMove][opponentMove] ++;
        for (int myMove = 0; myMove < 2; myMove++) {
            for (int opMove = 0; opMove < 2; opMove++) {
                gain = noCondMoves[myMove][opMove] * ap[opMove] * 
                       agentGain[myMove][opMove];
                if(gain > max) {
                    max = gain;
                    move = myMove;
                }
            }
        }
        myLastMove = move;
        return move;
    }

    public void startMatch() {
        for (int[] v : noCondMoves) {
            Arrays.fill(v, 1);
        }
    }
}
